package br.com.players.resource;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import br.com.players.model.Player;
import br.com.players.service.CadastroPlayerService;

@Path("/cadastro/player")
public class CadastroPlayer {

	@POST
	@Path("/inserir")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public String cadastrarPlayer(@FormParam("apelido") String apelido , @FormParam("nome") String nome, @FormParam("email") String email, @FormParam("senha") String senha ) {
		Player player = new Player();
		
		player.setApelido(apelido);
		player.setNome(nome);
		player.setEmail(email);
		player.setSenha(senha);
		
		CadastroPlayerService cps = new CadastroPlayerService();
		
		if (cps.playerExists(player)) {
			return "falha";
		} else {
			new CadastroPlayerService().inserirPlayer(player);
			return "sucesso";
		}
	}
	
	@GET
	@Path("/buscarPorNomeEmail")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public Player buscarPlayerNomeEmail(@QueryParam("nome") String nome, @QueryParam("email") String email) {
		return new CadastroPlayerService().buscarPlayerPeloNomeEmail(nome, email);
	}
	
	@GET
	@Path("/validar")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public Player fazerLogin(@QueryParam("email") String email , @QueryParam("senha") String senha) {
		return  new CadastroPlayerService().validarLogin(email, senha);
	}
	
	@GET
	@Path("/buscar/todos")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Player> buscarPlayers() {
		return new CadastroPlayerService().listarPlayers();
	}
	
	@GET
	@Path("/buscar")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public Player buscarPlayer(@QueryParam("id") String id) {
		return new CadastroPlayerService().buscarPlayerPeloid(id);
	}
	
	
	@POST
	@Path("/remover")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public void removerPArtida(@FormParam("id") String id) {
		Player player = new Player();
		player.setId(Integer.parseInt(id));
		
		new CadastroPlayerService().removerPlayer(player);
	}
	
	@POST
	@Path("/atualizar/player")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public void atualizarTime(@FormParam("id") String id,@FormParam("apelido") String apelido, @FormParam("nome") String nome, @FormParam("email") String email, @FormParam("senha") String senha) {
		Player player = new Player();
		
		player.setId(Integer.parseInt(id));
		player.setApelido(player.getApelido());
		player.setNome(player.getNome());
		player.setSenha(player.getSenha());
		
		new CadastroPlayerService().atualizarPlayer(player);
	}
	
	
}
